package tk.xboot.ws;

import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class OkHttpCli {

    public static void main(String[] args) throws IOException {
        startRequest();
    }


    private static final String url = "http://192.168.0.22:1915/appversion/getlatestversion?type=1&version=1.0.0";
    private static final int NORMAL_CLOSURE_STATUS = 1000;
    private static OkHttpClient cli;

    public static synchronized String startRequest() throws IOException {
        if (cli == null) {
            cli = new OkHttpClient.Builder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .authenticator(new Authenticator()
                    {
                        @Override
                        public Request authenticate(Route route, Response response) throws IOException
                        {//401，认证
                            String credential = Credentials.basic("zyao89", "password1");
                            return response.request().newBuilder().header("Authorization", credential).build();
                        }
                    })
                    .cookieJar(new CookieJar() {

                        /**
                         * Saves {@code cookies} from an HTTP response to this store according to this jar's policy.
                         *
                         * <p>Note that this method may be called a second time for a single HTTP response if the response
                         * includes a trailer. For this obscure HTTP feature, {@code cookies} contains only the trailer's
                         * cookies.
                         *
                         * @param url
                         * @param cookies
                         */
                        @Override
                        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                            System.out.println("cookies url: " + url.toString());
                            for (Cookie cookie : cookies) {
                                System.out.println("cookies: " + cookie.toString());
                            }
                        }

                        @Override
                        public List<Cookie> loadForRequest(HttpUrl url)
                        {//加载新的cookies
                            ArrayList<Cookie> cookies = new ArrayList<>();
                            Cookie cookie = new Cookie.Builder()
                                    .hostOnlyDomain(url.host())
                                    .name("SESSION").value("zyao89")
                                    .build();
                            cookies.add(cookie);
                            return cookies;
                        }
                    })
                    .build();

        }

        Request request = new Request.Builder() .url(url) .build();
        Response response = cli.newCall(request).execute();
        return response.body().string();// 返回的是string 类型
    }
}
